package haoyou.csnf.manager.module.pm.service.employeeeducations;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import haoyou.csnf.manager.module.pm.controller.admin.employeeeducations.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeeeducations.EmployeeEducationsDO;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;

import haoyou.csnf.manager.module.pm.dal.mysql.employeeeducations.EmployeeEducationsMapper;

import static haoyou.csnf.manager.framework.common.exception.util.ServiceExceptionUtil.exception;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.convertList;
import static haoyou.csnf.manager.framework.common.util.collection.CollectionUtils.diffList;
import static haoyou.csnf.manager.module.pm.enums.ErrorCodeConstants.*;

/**
 * 职工学历 Service 实现类
 *
 * @author 好有经验源码
 */
@Service
@Validated
public class EmployeeEducationsServiceImpl implements EmployeeEducationsService {

    @Resource
    private EmployeeEducationsMapper employeeEducationsMapper;

    @Override
    public Long createEmployeeEducations(EmployeeEducationsSaveReqVO createReqVO) {
        // 插入
        EmployeeEducationsDO employeeEducations = BeanUtils.toBean(createReqVO, EmployeeEducationsDO.class);
        employeeEducationsMapper.insert(employeeEducations);
        // 返回
        return employeeEducations.getId();
    }

    @Override
    public void updateEmployeeEducations(EmployeeEducationsSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeEducationsExists(updateReqVO.getId());
        // 更新
        EmployeeEducationsDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeEducationsDO.class);
        employeeEducationsMapper.updateById(updateObj);
    }

    @Override
    public void deleteEmployeeEducations(Long id) {
        // 校验存在
        validateEmployeeEducationsExists(id);
        // 删除
        employeeEducationsMapper.deleteById(id);
    }

    @Override
        public void deleteEmployeeEducationsListByIds(List<Long> ids) {
        // 校验存在
        validateEmployeeEducationsExists(ids);
        // 删除
        employeeEducationsMapper.deleteByIds(ids);
        }

    private void validateEmployeeEducationsExists(List<Long> ids) {
        List<EmployeeEducationsDO> list = employeeEducationsMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(EMPLOYEE_EDUCATIONS_NOT_EXISTS);
        }
    }

    private void validateEmployeeEducationsExists(Long id) {
        if (employeeEducationsMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_EDUCATIONS_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeEducationsDO getEmployeeEducations(Long id) {
        return employeeEducationsMapper.selectById(id);
    }

    @Override
    public PageResult<EmployeeEducationsDO> getEmployeeEducationsPage(EmployeeEducationsPageReqVO pageReqVO) {
        return employeeEducationsMapper.selectPage(pageReqVO);
    }

}